package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.SystemConfig;
import com.example.demo.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/systemConfig")
public class SystemConfigController {
    @Autowired
    private ConfigurableEnvironment environment;

    @RequestMapping("/setConfig")
    @ResponseBody
    public AjaxResult setConfig(@RequestBody String data){
        Map<String,String>  map= JSONObject.parseObject(data,Map.class);
        SystemConfig systemConfig=new SystemConfig();
        systemConfig.setIp(map.get("ip"));
        systemConfig.setPort(map.get("port"));
        systemConfig.setKeyIndex(map.get("keyIndex"));
        systemConfig.setKeyValue(map.get("keyValue"));
        systemConfig.setContainerName(map.get("containerName"));
        systemConfig.setPolicyName(map.get("policyName"));
        return  AjaxResult.SUCCESS();
    }

    @RequestMapping("/getConfig")
    @ResponseBody
    public AjaxResult getConfig(){
        Map<String,String> map=new HashMap<String,String>();
        map.put("ip",SystemConfig.getIp());
        map.put("port", SystemConfig.getPort());
        map.put("keyIndex",SystemConfig.getKeyIndex());
        map.put("keyValue",SystemConfig.getKeyValue());
        map.put("containerName", SystemConfig.getContainerName());
        map.put("policyName", SystemConfig.getPolicyName());
      return AjaxResult.SUCCESS(map);
    }




}
